package com.rrd.itext.html;

import java.util.ArrayList;
import java.util.List;

import com.itextpdf.text.Chunk;
import com.itextpdf.text.Element;
import com.itextpdf.tool.xml.NoCustomContextException;
import com.itextpdf.tool.xml.Tag;
import com.itextpdf.tool.xml.WorkerContext;
import com.itextpdf.tool.xml.exceptions.RuntimeWorkerException;
import com.itextpdf.tool.xml.html.HTML;
import com.itextpdf.tool.xml.html.HTMLUtils;
import com.itextpdf.tool.xml.html.Span;

public class ITextServiceSpan extends Span{
	@Override
	public List<Element> content(WorkerContext ctx, Tag tag, String content){
		if(HTML.Tag.SPAN.equalsIgnoreCase(tag.getName())){
				String sanitized = HTMLUtils.sanitizeInline(content);
		    	List<Element> l = new ArrayList<Element>(1);
		    	if (sanitized.length() > 0) {
					try {
						l.add(getCssAppliers().apply(new Chunk(sanitized), tag, getHtmlPipelineContext(ctx)));
					} catch (NoCustomContextException e) {
						throw new RuntimeWorkerException(e);
					}
		    	}
		    	return l;
		}
		return super.content(ctx, tag, content);
	}

    @Override
    public List<Element> end(WorkerContext ctx, Tag tag, List<Element> currentContent) {
        if(HTML.Tag.SPAN.equalsIgnoreCase(tag.getName())){
            return currentContent;
        }
        return super.end(ctx, tag, currentContent);
    }
	
	
}
